package com.example.autopia.view.ViewHolders.News

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.DrawableImageViewTarget
import com.example.autopia.R
import com.example.autopia.databinding.ViewholderNewsPostOutPhotoBinding
import com.example.autopia.model.Item.News
import com.example.autopia.utilities.ImageManager
import com.example.autopia.view.Listeners.PostNewsListener
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase

class TextNewsViewHolder(
    private val binding: ViewholderNewsPostOutPhotoBinding,
    private val inflater: LayoutInflater,
    private val likeNews: MutableList<String>,
) :
    RecyclerView.ViewHolder(binding.root) {

    private val imageManager = ImageManager()
    private val firebaseAuth = Firebase.auth

    fun bind(news: News, listener: PostNewsListener) {
        setData(news, listener)
        val popupView = inflater.inflate(R.layout.news_popup_layout, null)
        val popupWindow = PopupWindow(
            popupView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            true
        )
        Log.d("Yatoro", news.userverificated.toString())

        if (news.userverificated != 0) {
            binding.ivVerificated.setImageResource(R.drawable.icon_verificated)
        } else {
            binding.ivVerificated.setImageResource(R.drawable.icon_empty)
        }

        val enterAnimation =
            AnimationUtils.loadAnimation(inflater.context, R.anim.popup_enter_animation)
        popupView.startAnimation(enterAnimation)
        val delete = popupView.findViewById<LinearLayout>(R.id.delete)
        val delete2 = popupView.findViewById<LinearLayout>(R.id.delete2)

        if (news.userid == (firebaseAuth.currentUser?.uid ?: "")){
            delete2.setOnClickListener {
                listener.onDeleteClick(news)
            }
        }else{
            delete2.visibility = View.GONE
        }

        delete.setOnClickListener {
            listener.onMoreClick(news)
            popupWindow.dismiss()
        }

        binding.btMore.setOnClickListener {
            popupWindow.showAsDropDown(binding.btMore)
        }

        val animation = AnimationUtils.loadAnimation(inflater.context, R.anim.fade_in)
        binding.root.startAnimation(animation)
    }

    private fun setData(news: News, listener: PostNewsListener) {
        binding.apply {

            Glide.with(itemView)
                .load(imageManager.byteArrayToBitmap(news.logo))
                .centerCrop()
                .placeholder(R.drawable.logo_placeholder)
                .error(R.drawable.logo_placeholder)
                .into(ivLogo)

            val list = likeNews.filter { id -> id.contains(news.id)}
            if (list.isNotEmpty()){
                ivLike.setImageResource(R.drawable.icon_like_purple_bold) //Сделать лайки
            }

            tvName.text = news.name
            tvTitle.text = news.title
            tvMainText.text = news.mainText
            tvLike.text = news.likes.toString()
            tvComment.text = news.comments?.size.toString()

            btLike.setOnClickListener{
                val list = likeNews.filter { id -> id.contains(news.id)}
                if (list.isNotEmpty()){
                    likeNews.remove(news.id)
                    ivLike.setImageResource(R.drawable.icon_like_grey_stroke)
                    listener.onAddLikeClick(news,false)
                }else{
                    likeNews.add(news.id)
                    Glide.with(inflater.context).load(R.drawable.anim_like)
                        .into<DrawableImageViewTarget>(DrawableImageViewTarget(ivLike))
                    listener.onAddLikeClick(news,true)
                }
            }
            btComment.setOnClickListener {
                listener.onAddCommentClick(news)
            }
            ivLogo.setOnClickListener {
                listener.onChannelCLick(news.userid)
            }
        }
    }
}